package thread;

import java.util.concurrent.CountDownLatch;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:34398
 * Date:2024-07-18
 * Time:10:06
 */


class Conter {
    public int count = 0;

   synchronized public void add() {//可以在类里面用这个关键字修饰方法
        count++;
    }
}

public class Synchronized_Class {

    public static void main(String[] args) throws InterruptedException {
        Conter conter = new Conter();
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                conter.add();
            }
        });

        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                conter.add();
            }
        });

        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();

        System.out.println(conter.count);
    }
}
